package com.city.parking;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baidu.mapapi.search.core.PoiInfo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class OrderListActivity extends FragmentActivity implements
        OnClickListener {
	private List<OrderItem> mOrderList = new ArrayList<OrderItem>();
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private OrderListAdapter mAdapter;
	private Activity me;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        me = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_order_list);
		TextView titleTv = (TextView) findViewById(R.id.nav_title);
	    titleTv.setText("我的订单");

        ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
        back.setOnClickListener(this);

        ImageView more = (ImageView) findViewById(R.id.nav_ico_more);
        more.setVisibility(View.GONE);
        
        mOrderList.add(new OrderItem("仙龙湾1车库", 10, System.currentTimeMillis())); 
        mOrderList.add(new OrderItem("中央商场停车场", 15, System.currentTimeMillis()));
        mOrderList.add(new OrderItem("南京新百地下停车场", 25, System.currentTimeMillis()));
        mOrderList.add(new OrderItem("沃尔玛停车场", 5, System.currentTimeMillis()));
        mOrderList.add(new OrderItem("盛世花园对外停车场", 10, System.currentTimeMillis()));
        
        ListView listView = (ListView) findViewById(R.id.order_list);
        mAdapter = new OrderListAdapter(this);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int pos,
                    long arg3) {
                OrderItem item = mOrderList.get(pos);
                
                Intent i = new Intent(me, OrderDetailActivity.class);
                i.putExtra("name", item.name);
                i.putExtra("price", item.price);
                
                startActivity(i);
            }

        });
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nav_ico_back:
			finish();
            break;
		default:
			break;
		}
	}
	
	private class ViewHolder {
		TextView name;
		TextView price;
		TextView date;
		String dateFmt;
		TextView status;
	}
	
	private class OrderListAdapter extends BaseAdapter {
		private LayoutInflater inflater = null;

		public OrderListAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mOrderList.size();
		}

		@Override
		public Object getItem(int position) {
			return mOrderList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.order_item, null);
				holder.name = (TextView) convertView
				        .findViewById(R.id.order_parking);
				holder.price = (TextView) convertView
				        .findViewById(R.id.order_price);
				holder.date = (TextView) convertView
				        .findViewById(R.id.order_date);
				holder.status = (TextView) convertView
                        .findViewById(R.id.order_status);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			OrderItem item = (OrderItem)getItem(position);
			if (item != null) {
				holder.name.setText(item.name);
				holder.price.setText(item.price+"");
				if (holder.dateFmt == null) {
					holder.dateFmt = sdf.format(new Date(item.date));
				}
				holder.date.setText(holder.dateFmt);
				holder.status.setText(item.getStatus());
			}
			return convertView;
		}
	}
	
}
